package com.example.problem1;

import com.example.problem1.api.DemoFactory;
import com.example.problem1.api.impl.DemoFactoryBean;
import com.example.problem1.entity.Demo;
import com.example.problem1.api.impl.AutowireFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ServiceLoader;

public class XmlBean {

    public static void main(String[] args) {

        // 使用xml装配
        ApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:demo-application.xml");
        // 静态方法装配
        System.out.println(Demo.createDemo());
        // 使用setter方法装配
        Demo demo1 = xmlApplicationContext.getBean("demo1", Demo.class);
        System.out.println(demo1);
        // 构造方法
        Demo demo2 = xmlApplicationContext.getBean("demo2", Demo.class);
        System.out.println(demo2);

        // 自定义factoryBean
        Demo demo4 = xmlApplicationContext.getBean("demoFactoryBean", Demo.class);
        System.out.println(demo4);

        // AutowireFactory装配
        AutowireCapableBeanFactory autowireCapableBeanFactory = xmlApplicationContext.getAutowireCapableBeanFactory();
        DemoFactory demoFactory = autowireCapableBeanFactory.createBean(AutowireFactory.class);
        System.out.println(demoFactory.createDemo());
    }
}
